#include "StdAfx.h"
#include "ShaderManager.h"


ShaderManager::ShaderManager(void)
{
}


ShaderManager::~ShaderManager(void)
{
}

GLuint ShaderManager::CreateShader(GLenum type, const char* pSource)
{
    GLuint shader = glCreateShader(type);
    glShaderSource(shader, 1, &pSource, 0);
    glCompileShader(shader);
    //printShaderInfoLog(shader);
    return shader;
}

bool ShaderManager::CheckShader(GLint shader)
{
	GLint length;
    glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &length);
    if (length)
    {
        //char* buffer = (char*)malloc( sizeof(char) * length ) ;
       // glGetShaderInfoLog(shader, length, NULL, buffer);
        //printf("%s", buffer);
        //free( buffer ) ;
        GLint success;
        glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
        if(success != GL_TRUE)
		{
            return false;
        }
    }

	return true;
}

bool ShaderManager::CompileShaders(GLuint *shaderProgram, const char* vertex, const char *fragment)
{
    *shaderProgram = glCreateProgram();

    GLuint vertexShader = CreateShader(GL_VERTEX_SHADER, vertex);
	if(CheckShader(vertexShader) == false)
	{
		return false;
	}

    GLuint fragmentShader = CreateShader(GL_FRAGMENT_SHADER, fragment);
	if(CheckShader(fragmentShader) == false)
	{
		return false;
	}

    glAttachShader(*shaderProgram, vertexShader);
    glAttachShader(*shaderProgram, fragmentShader);

    glLinkProgram(*shaderProgram);

    glUseProgram(*shaderProgram);

	return true;
}

